package com.example.dbpractise1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.model.Person;
import com.example.service.PersonService;

public class PersonActivity extends Activity {
	private static final String TAG = "PersonActivity";
	private static ArrayList<Person> list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		PersonService ps = new PersonService(this);
		ps.update(new Person().setName("yangzhigang").setPersonId(37));
		
		
		ListView lView = (ListView) this.findViewById(R.id.listView);
		PersonAdapter adapter = this.new PersonAdapter(this);
		lView.setAdapter(adapter);
		
		PersonButtonOnClickListener pbOnClickListener = new PersonActivity.PersonButtonOnClickListener(this, adapter);
		Button insert = (Button) findViewById(R.id.button);
		insert.setOnClickListener(pbOnClickListener);
		
		Button delete = (Button) findViewById(R.id.button1);
		delete.setOnClickListener(pbOnClickListener);
		
		Button update = (Button) findViewById(R.id.button2);
		update.setOnClickListener(pbOnClickListener);	
		
		Button qurey = (Button) findViewById(R.id.button3);
		qurey.setOnClickListener(pbOnClickListener);	
		
		lView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	private class PersonAdapter extends BaseAdapter {
		
		private LayoutInflater mInflater;
		
		public PersonAdapter(Context context) {
			PersonService ps = new PersonService(context);
			list = (ArrayList<Person>) ps.qurey(PersonService.NO_VALUE);
			mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Person p = (Person) list.get(position);
			
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item, parent, false);
			}
			
			TextView id = (TextView) convertView.findViewById(R.id.personid);
			id.setText(String.valueOf(p.getPersonId()));
			
			TextView name = (TextView) convertView.findViewById(R.id.name);
			name.setText(p.getName());
			
			TextView amount = (TextView) convertView.findViewById(R.id.amount); 
			amount.setText(String.valueOf(p.getAmount()));
			
			return convertView;
		}

		
	}
	
	private static class PersonButtonOnClickListener implements OnClickListener {
		private ContentResolver cResolver;
		private PersonAdapter adapter;
		private PersonService ps;
		private static final String AUTHORITIES = "com.example.providers.personprovider";
		private static final Uri AUTHORITIES_URI = Uri.parse("content://" + AUTHORITIES);
		private static final Uri LIST_URI = Uri.withAppendedPath(AUTHORITIES_URI, "person");
		private static final Uri DATA_URI = ContentUris.withAppendedId(LIST_URI, 11); 
		
		public PersonButtonOnClickListener(Context context, PersonAdapter adapter) {
			cResolver = context.getContentResolver();
			this.adapter = adapter;
			ps = new PersonService(context);
		}
		
		@Override
		public void onClick(View v) {
//			cResolver = PersonActivity.this.getContentResolver();
			ContentValues values = new ContentValues();

			switch(v.getId()) {
			
			case R.id.button:
				values.clear();
				values.put("name", "fantuan");
				values.put("amount", 2000);
				cResolver.insert(LIST_URI, values);
				list = (ArrayList<Person>) ps.qurey(PersonService.NO_VALUE);
				adapter.notifyDataSetChanged();
				return;
				
			case R.id.button1:
				cResolver.delete(LIST_URI, null, null);
				list = (ArrayList<Person>) ps.qurey(PersonService.NO_VALUE);
				adapter.notifyDataSetChanged();
				break;
				
			case R.id.button2:
				values.clear();
				values.put("amount", 10);
				cResolver.update(LIST_URI, values, "name=?", new String[] {"fantuan"});
				list = (ArrayList<Person>) ps.qurey(PersonService.NO_VALUE);
				adapter.notifyDataSetChanged();
				break;
				
			case R.id.button3:
				Cursor cursor = cResolver.query(LIST_URI, null, null, null, null);
//				list.clear();
//				while(cursor.moveToNext()) {
//					list.add(new Person().setPersonId(cursor.getInt(cursor.getColumnIndex("_id"))).
//							setName(cursor.getString(cursor.getColumnIndex("name"))).
//							setAmount(cursor.getLong(cursor.getColumnIndex("amount"))));
//				}
				break;
			default:
				break;
			}
		}
		
		private void setContentValues(ContentValues values, HashMap<String, Object> maps) {
			values.clear();
			
			if (maps != null && maps.size() != 0) {
				Set<String> sets = maps.keySet();
				
				for(String key:sets) {
					
					if (key.equals("name")) {
						values.put(key, (String)maps.get(key));
					}else if (key.equals("amount")) {
						values.put(key, (Long)maps.get(key));
					}
				}
			}
	}
		
	}
}
